﻿using System.Collections.Generic;
using System.Linq;
using Model.Core;
using Models;
using SIE.SCADA.Helper;
using SIE.SCADA.SqlSugar;
using SIE.SCADA.ZemtDemo;
using SIE.SCADA.ZemtDemo.Common;
using SIE.SCADA.ZemtDemo.MqClient.Model;

namespace SCADA_DAQ.ZEMT.Server
{
    /// <summary>
    /// 上报工时
    /// </summary>
    public class PlanManager : MqMangerBase<PlanManager>
    {
        private smdc_disid_t _Disid_T;
        public string linename = Env.LineName.Value;
        private readonly string IfaceName = "PRODUCTREPORT"; //接口名称
        private readonly string EXCHANAGE = "ZEMT_MQ_EXCHANGE_PRODUCTREPORT";
        private readonly string ROUTEKEY = "PRODUCTREPORT_SIE";
        private AdvancedThread thread;

        //修改状态，请求上传配置信息
        public void CheckUpload(List<smdc_disid_t> smdc_disids)
        {
            var upload = smdc_disids.FirstOrDefault();
            _Disid_T = upload;
            if (upload != null)
            {
                List<WorksndataItem> worksndataItems = new List<WorksndataItem>();
                foreach (var item in smdc_disids)
                {
                    var sndata = new WorksndataItem()
                    {
                        sn = item.serial_number
                    };
                    worksndataItems.Add(sndata);
                }
                List<WorkorderdatasItem> tempdatasItems = new List<WorkorderdatasItem>();
                List<MaininfosItem> maininfosItems = new List<MaininfosItem>();
                WorkorderdatasItem tempdatasItem = new WorkorderdatasItem();
                tempdatasItem.disid = upload.disidno;              
                tempdatasItem.po_no = upload.poid;
                tempdatasItem.matid = upload.matid;
                tempdatasItem.matdesc = upload.matdesc;
                tempdatasItem.workbatch = string.Empty;//批号
                tempdatasItem.worktype = string.Empty;//报工类型
                tempdatasItem.workerid = string.Empty;//报工人员
                tempdatasItem.workqty = string.Empty;//报工数量
                tempdatasItem.laborhour = string.Empty;//工时
                tempdatasItem.workseq = upload.workbin;//工序
                tempdatasItem.workseqdesc = upload.workbinname;//工序描叙
                tempdatasItem.workseq_group = string.Empty;//工序组
                tempdatasItem.workseq_groupdesc = string.Empty;//工序组描叙
                tempdatasItem.worksndata = worksndataItems;
                tempdatasItems.Add(tempdatasItem);
                PlanData senddata = new PlanData() { workorderdatas = tempdatasItems };
                Send(senddata);
            }
        }

        //报工请求
        void Send(PlanData senddata)
        {
            var res = mqhelper.TopicExchangePublish(senddata, EXCHANAGE, "", $"{IfaceName}.{linename}"
                , true, false, true, false);
            if (res.Success)
                System.Diagnostics.Debug.WriteLine("数据已推送完成的！");
            else
                System.Diagnostics.Debug.WriteLine("数据推送失败！\n" + res.Message);
        }

        private void Loop()
        {
            var pgsql = new PgSqlAccess();
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    var upload = ormClient.SqlSugarClient.Queryable<smdc_disid_t>().Where(m => m.state_int == (int)State.UpdLoad).ToList();
                    CheckUpload(upload);
                }
            }
        }

        public override void Start()
        {
            DataReceive();
            thread = new AdvancedThread(Loop, 10000);
            thread.Start();
        }

        public override void Stop()
        {
            thread.Stop();
            base.Stop();
        }

        void DataReceive()
        {
            //报工接口
            var res1 = mqhelper.TopicExchangeConsumer<BackResult>((data) =>
            {
                if(data.retcode.Equals("1"))
                {
                    System.Diagnostics.Trace.WriteLine("配置结果接收成功");
                }
                return true;
            }, EXCHANAGE, $"ZEMT_MQ_SIE_{IfaceName}", $"{ROUTEKEY}.{linename}", true, false, true, false);
        }

        public class Retdatas
        {
            /// <summary>
            /// 生产订单号
            /// </summary>
            public string pono { get; set; }
            /// <summary>
            /// 产品物料号
            /// </summary>
            public string pmid { get; set; }
            /// <summary>
            /// 产品序列号
            /// </summary>
            public string productsn { get; set; }
            /// <summary>
            /// 子物料号
            /// </summary>
            public string mid { get; set; }
            /// <summary>
            /// 错误信息
            /// </summary>
            public string errormsg { get; set; }
        }

        public class BackResult
        {
            /// <summary>
            /// 
            /// </summary>
            public string retcode { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string retmsg { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public List<Retdatas> retdatas { get; set; }
        }

    }
}
